<?php 
	require_once 'xml_regex.php';
	require_once('simple_html_dom.php');
class NewsFeed{

	protected $_username 	= 'root';
	protected $_password 	= '';
	protected $_db 			= 'shopbebo';
 	
	protected $_host     = 'localhost';
	protected $_connect;
	protected $_flagConnect;
	
	protected $_filter;
	protected $_mail;
	

	public function __construct() {		
		$this->_connect = mysql_connect ( $this->_host, $this->_username, $this->_password );
		
		if(!$this->_connect) {
			die('Could not connect: ' . mysql_error());
		}else{
			$this->_connect = $this->_connect;
			$this->_flagConnect = true;
			mysql_select_db($this->_db, $this->_connect);
		}
	
	}
	public function query($sql){
		if ($this->_flagConnect == true) {
			mysql_query("SET NAMES 'utf8'");
			mysql_query("SET CHARACTER SET 'utf8'");
			$this->result = mysql_query($sql);
			return $this->result;
		}
	}
	
	function getFeed(){
		$arrFeed = $this->listFeed();
		if(!empty($arrFeed)){
			foreach ($arrFeed as $k => $v){
				$url 		= $v;
				$xml 		= file_get_contents($url);
				
				$news_items = element_set('item', $xml);
				
				foreach($news_items as $item) {
					$title 	= value_in('title', $item);
					$url 	= value_in('link', $item);
					$description = value_in('description', $item);
					$description = str_replace('<![CDATA[', '', $description);
					$description = str_replace(']]>', '', $description);
					$description = str_replace('&lt;', '<', $description);
					$description = str_replace('&gt;', '>', $description);
					$description = str_replace('&quot;', '"', $description);
					echo $description = str_replace('read more', '', $description);
					
					preg_match('#src="([^"]*)"#imsU', $description, $matches);
					echo '<pre>';
					print_r($matches);
					echo '</pre>';
					
					$timestamp = strtotime(value_in('pubDate', $item));
					$author = value_in('dc:creator', $item);
					
// 					echo $this->getContent('http://giaitri.vnexpress.net/tin-tuc/thoi-trang/lam-dep/my-linh-tre-trung-pha-cach-nho-ultherapy-2876593.html');
					die();
					$item_array[] = array(
							'title' => str_replace('"', '\"', $title),
							'url' => $url,
							'summary' => strip_tags($description),
							'content' => $this->getContent($url),
							'author' => $author,
							'created' => date('Y-m-d H:i:s', $timestamp),
							'created_by' => 1,
							'status' => 'active',
					);
					
					if($this->checkExist($title) != true){
//						$this->saveNews($item_array);
					}
				}
			}
		}
		
		echo '<pre>';
		print_r($item_array);
		echo '</pre>';
	}
	
	function listFeed($params = null){
		/*$sql = 'SELECT *
				FROM `news_feed`
				WHERE (`status` ="active")
				ORDER BY `id` ASC';
		$result = $this->query($sql);
	
		$newArray = array();
		while($row = mysql_fetch_assoc($result)){
			$newArray[] = $row;
		}*/
		$newArray[] = 'http://ngoisao.net/rss/thoi-trang.rss';
		return $newArray;
	}
	
	function checkExist($title){
		$sql = 'SELECT *
				FROM `news_item`
				WHERE (`name` = "' . $title . '")';
		$result = $this->query($sql);
		$row = mysql_fetch_row($result);
		
		if(!empty($row)){
			return true;
		}
		return false;
	}
	
	function saveNews($params = null){
		$title 		= $params["title"];
		$author 	= $params["author"];
		$summary 	= $params["summary"];
		$content 	= str_replace("'", "\'", $params["content"]);
		$created 	= $params["created"];
		$created_by = $params["created_by"];
		$status 	= $params["status"];
		
		$sql = "INSERT INTO news_item
							(name, author, summary, content, created, created_by, status)
						VALUES
							('$title','$author','$summary','$content','$created','$created_by','$status')";
		$result = $this->query($sql);
	}
	
	public function getContent($url){
		if(empty($url))
			return false;
		$tmp = parse_url($url);
		$domain = $tmp['scheme'] . '//' . $tmp['host'];
		
		$html = @file_get_contents($url);
		$html = str_get_html($html);
		$xhtml = $html->find('div.fck_detail', 0)->xmltext;
			
		$xhtml = preg_replace('#href="([^(http)(https)][^"]*)"#', 'href="' . $domain . '$1"', $xhtml);
		$xhtml = preg_replace('#src="([^(http)(https)][^"]*)"#', 'src="' . $domain . '$1"', $xhtml);
		$xhtml = preg_replace('#<ul class="pager"></ul>#', '', $xhtml);
		$xhtml = preg_replace('#<ul class="pager"></ul>#', '', $xhtml);
		$xhtml = preg_replace('#<p style="text-align:right;">.*</p>#', '', $xhtml);
		$xhtml = preg_replace('#<table align="center" border="1" cellpadding="1" cellspacing="1" style="width:500px;">.*</table>#', '', $xhtml);
		return $xhtml;
	}
}
	
	
	
	
	
	
	
	
	
	
	
	
	